"""
从数据库导出到big文件
"""
import json
import time

import facade
from logicaltool.BaseTools.file_tools import FileTools
from pymongo import MongoClient
from xjlibrary.our_file_dir import BaseDir

curPath = BaseDir.get_file_dir_absolute(__file__)
upPath = BaseDir.get_upper_dir(curPath, -1)
configfile = BaseDir.get_new_path(upPath, "db.ini")
topPath = BaseDir.get_upper_dir(upPath, -2)
bigpath = BaseDir.get_new_path(topPath, "download", "sipogov", "download", "bigjson")
BaseDir.create_dir(bigpath)


class ImportOut(FileTools):
    def __init__(self):
        super().__init__()
        self.logger = facade.get_streamlogger()
        self.connection = MongoClient(
            "mongodb://xujiangrw:vipdatacenter@192.168.30.171:27017",
            # 如果“True”且服务器正在运行而没有日志记录，阻塞，直到服务器将所有数据文件同步到磁盘为止。
            fsync=False
        )
        self.db = self.connection['cnipa']

    def get_id(self):
        """
        得到requests以便获取数据
        日期对于学校没有意义 故用他来区分学校
        20181201 北京交通大学 的英文和中文
        20181130 重大的英文
        20181129 华东理工大学
        :return:
        """
        results = self.db.pub_all_id_school.find({"date": "20181129"})
        for row in results:
            self.inputobj1.add(row["requestsid"])
        allcount = len(self.inputobj1)
        i = 0
        with open(
                BaseDir.get_new_path(bigpath, time.strftime('%Y%m%d%H%M%S', time.localtime(time.time())) + ".big_json"),
                'w', encoding="utf-8") as f:
            for requestsid in self.inputobj1:
                print(requestsid)
                absfull = self.db.absfull.find_one({"requestsid": requestsid})
                absfull.pop("_id")
                info = self.db.patentinfo.find_one({"requestsid": requestsid})
                absfull["patentinfo"] = info["patentinfo"]
                absfull["cmsg"] = info["cmsg"]
                absfull["lawmsg"] = info["lawmsg"]
                absfull["pmsg"] = info["pmsg"]
                absfull["cpmsg"] = info["cpmsg"]
                absfull["ccount"] = info["ccount"]
                absfull["pcount"] = info["pcount"]
                absfull["tcount"] = info["tcount"]
                absfull["cpnum"] = info["cpnum"]
                allid = self.db.pageidjson.find_one({"requestsid": requestsid})
                absfull["msg"] = allid["pageidinfo"]
                jsonmsg = json.dumps(absfull, ensure_ascii=False)
                f.write(jsonmsg + "\n")
                i += 1
                print("{}/{}".format(i, allcount))

    def get_id_1(self):
        """
        应急处理
        :return:
        """
        results = self.db.pub_all_id_school.find({"date": "20181201"})
        for row in results:
            self.inputobj1.add(row["requestsid"])

        results = self.db.patentinfo.find({"stat": 0})
        for row in results:
            self.inputobj2.add(row["requestsid"])

        for requests in (self.inputobj1 & self.inputobj2):
            self.db.patentinfo.update({'requestsid': requests}, {"$set": {'stat': 2}})


if __name__ == "__main__":
    im = ImportOut()
    im.get_id()
